﻿namespace BM.Common
{
    using System.Collections.Generic;
    using System.IO;
    using Newtonsoft.Json;

    /// <summary>
    /// This class contain config infomation of Bluestack Manager
    /// </summary>
    public class BMConfig
    {
        /// <summary>
        /// Backup collection
        /// </summary>
        public List<string> BackUps { get; set; }
        /// <summary>
        /// Đường dẫn đến các tệp tin, thư mục data của BlueStack
        /// Mặc định : C:\ProgramData\BlueStack
        /// </summary>
        public string DataPaths { get; set; }
        /// <summary>
        /// Repository when create new backup data
        /// </summary>
        public string BackUpBase { get; set; }

        /// <summary>
        /// Initialize new Bluestack manager configuration
        /// </summary>
        private BMConfig()
        {
            BackUps = new List<string>();
        }
        /// <summary>
        /// Load BM Setting
        /// </summary>
        /// <returns>BMConfig instance contain all infomation need for bluestack work</returns>
        public static BMConfig LoadSetting()
        {
            BMConfig bmC = null;
            if (!File.Exists("bm.ini")) {
                // Create new BMConfig and save
                bmC = new BMConfig();
                SaveSetting(bmC);
            }
            else {
                // Load existed config file
                var json = File.ReadAllText("bm.ini");
                bmC = JsonConvert.DeserializeObject<BMConfig>(json);
            }
            return bmC;
        }
        /// <summary>
        /// Save bm config setting
        /// </summary>
        /// <param name="bmConfig"></param>
        public static void SaveSetting(BMConfig bmConfig)
        {
            var json = JsonConvert.SerializeObject(bmConfig);
            File.WriteAllText("bm.ini", json);
        }
    }
}
